package com.wang;

import com.wang.service.IDemoService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.protocol.dubbo.FutureAdapter;

public class AppTestCallback {
    public static void main(String[] args) throws Exception{
        //创建服务应用对象实例
        ReferenceConfig<IDemoService> referenceConfig = new ReferenceConfig<>();
        //设置应用程序信息
        referenceConfig.setApplication(new ApplicationConfig("dubbo-consumer"));
        //设置服务注册中心
        referenceConfig.setRegistry(new RegistryConfig("nacos://127.0.0.1:8848"));
        //设置服务接口和超时时间
        referenceConfig.setInterface(IDemoService.class);
        referenceConfig.setTimeout(3000);
        //设置负载策略和集群容错策略
        //referenceConfig.setLoadbalance("myLoadBalance");
        //referenceConfig.setCluster("myBroadcast");
        //设置服务的版本号和分组
        referenceConfig.setVersion("1.0.0");
        referenceConfig.setGroup("dev");
        //设置为异步调用
        referenceConfig.setAsync(true);
        //引用服务
        IDemoService demoService = referenceConfig.get();
        //设置隐式参数
        RpcContext.getContext().setAttachment("company","xiaomi");
        String msg = demoService.getMsg("dev");
        System.out.println(msg);
        FutureAdapter<Object> futureAdapter = (FutureAdapter)RpcContext.getContext().getFuture();
        futureAdapter.whenComplete((result,e)->{
            if(e == null){
                System.out.println(result);
            }else{
                System.out.println(e.getLocalizedMessage());
            }
        });
        System.out.println("main thread end");
        //添加线程阻塞，防止整个进程结束，异步线程也被结束
        Thread.currentThread().join();
    }
}
